package com.qth.valet.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qth.valet.entity.po.Orders;
import com.qth.valet.entity.vo.OrdersDetail;
import com.qth.valet.entity.vo.OrdersPending;
import com.qth.valet.mapper.provider.OrderProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

public interface OrdersMapper extends BaseMapper<Orders> {

    @Select("select \n" +
            "CONCAT(d.surname, d.name) dname,\n" +
            "d.driver_age,\n" +
            "d.phone,\n" +
            "d.profile_img,\n" +
            "o.current_addr,\n" +
            "o.target_addr,\n" +
            "o.price,\n" +
            "TIMESTAMPDIFF(MINUTE, o.reach_time, o.start_time) wait_time,\n" +
            "TIMESTAMPDIFF(MINUTE, o.start_time, o.end_time) all_time,\n" +
            "ROUND((ST_Distance_Sphere(\n" +
            "    POINT(current_lo, current_la), \n" +
            "    POINT(target_lo, target_la)   \n" +
            ") / 1000), 2) distance\n" +
            "from orders o\n" +
            "inner join driver d\n" +
            "where o.driver_id = d.id\n" +
            "and o.id=#{orderId}")
    OrdersDetail getOrderDetail(String orderId);

    @Select("select  \n" +
            "\to.id, \n" +
            "\tu.username,\n" +
            "\tu.avatar,\n" +
            "\tu.phone,\n" +
            "\to.order_time, \n" +
            "\to.hope_time, \n" +
            "\to.current_addr, \n" +
            "\to.current_lo, \n" +
            "\to.current_la, \n" +
            "\to.target_addr, \n" +
            "\to.target_lo, \n" +
            "\to.target_la, \n" +
            "\to.price, \n" +
            "\tRound((ST_Distance_Sphere(\n" +
            "\t\tpoint(o.current_lo, o.current_la),\n" +
            "\t\tpoint(#{longitude}, #{latitude})\n" +
            "\t) / 1000), 2) distance \n" +
            "from orders o\n" +
            "inner join users u\n" +
            "on o.user_id = u.id\n" +
            "where o.status = #{status}\n" +
            "order by distance, o.order_time")
    List<OrdersPending> getPendingOrder(Double longitude, Double latitude, int status);

    /*@Select("select \n" +
            "\to.*,\n" +
            "u.username,\n" +
            "u.avatar \n" +
            "from orders o\n" +
            "inner join users u\n" +
            "on u.id = o.user_id\n" +
            "where driver_id = #{driverId}\n" +
            "order by o.status, o.id desc")*/
    @SelectProvider(type = OrderProvider.class, method = "selectOrderByStatus")
    IPage<Orders> getAllOrder(Integer driverId, Page<Orders> page, Integer status);

    @Select("SELECT\n" +
            "\to.id,\n" +
            "\to.business_type,\n" +
            "\to.current_addr,\n" +
            "\to.current_la,\n" +
            "\to.current_lo,\n" +
            "\to.target_addr,\n" +
            "\to.target_la,\n" +
            "\to.target_lo,\n" +
            "\to.driver_id,\n" +
            "\to.end_time,\n" +
            "\to.hope_time,\n" +
            "\to.order_time,\n" +
            "\to.price,\n" +
            "\to.reach_time,\n" +
            "\to.receive_time,\n" +
            "\to.remark,\n" +
            "\to.start_time,\n" +
            "\to.`status`,\n" +
            "\to.user_id,\n" +
            "\tu.username,\n" +
            "\to.user_phone,\n" +
            "\td.driver_age,\n" +
            "\td.profile_img,\n" +
            "\td.surname,\n" +
            "\td.`name`,\n" +
            "\td.phone driver_phone,\n" +
            "\td.`status` driver_status \n" +
            "FROM\n" +
            "\torders o\n" +
            "\tLEFT JOIN users u ON o.user_id = u.id\n" +
            "\tLEFT JOIN driver d ON o.driver_id = d.id \n" +
            "WHERE\n" +
            "\to.id = #{orderId}")
    Orders getOrderById(String orderId);
}
